package com.gitee.cirnochat.file.controller;

import com.gitee.cirnochat.common.annotation.AuthCheck;
import com.gitee.cirnochat.common.model.vo.resp.Result;
import com.gitee.cirnochat.common.utils.RequestUtils;
import com.gitee.cirnochat.file.model.vo.req.EmojiPackItemUpdateRequest;
import com.gitee.cirnochat.file.model.vo.resp.EmojiPackItemCreateResponse;
import com.gitee.cirnochat.file.model.vo.resp.EmojiPackItemResponse;
import com.gitee.cirnochat.file.model.vo.resp.EmojiPackItemUpdateResponse;
import com.gitee.cirnochat.file.model.vo.resp.EmojiPackResponse;
import com.gitee.cirnochat.file.service.EmojiPackService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Tag(name = "表情包相关接口", description = "表情包相关API")
@RestController
@Validated
@RequiredArgsConstructor
@RequestMapping("/emoji")
public class EmojiController {
    private final EmojiPackService emojiPackService;


    @Deprecated
    @Operation(summary = "获取用户表情包列表", description = "获取当前用户的表情包列表")
    @ApiResponse(
            responseCode = "200",
            description = "成功",
            content = @Content(
                    mediaType = "application/json",
                    array = @ArraySchema(
                            schema = @Schema(implementation = EmojiPackResponse.class, description = "表情包列表")
                    )
            )
    )
    @GetMapping("/pack/list")
    @AuthCheck
    @Parameter(name = "Authorization", description = "认证信息", required = true , in = ParameterIn.HEADER)
    @Parameter(name = "X-Device-Id", description = "设备ID", required = true , in = ParameterIn.HEADER)
    public Result<List<EmojiPackResponse>> listUserEmojiPacks(HttpServletRequest request) {
        Long userId = RequestUtils.getUserId(request);
        List<EmojiPackResponse> emojiPacks = emojiPackService.listUserEmojiPacks(userId);
        return Result.success(emojiPacks);
    }


    @Operation(summary = "获取用户表情包信息", description = "获取当前用户的表情包信息")
    @ApiResponse(
            responseCode = "200",
            description = "成功",
            content = @Content(
                    mediaType = "application/json",
                    array = @ArraySchema(
                            schema = @Schema(implementation = EmojiPackResponse.class, description = "表情包列表")
                    )
            )
    )
    @GetMapping("/pack")
    @AuthCheck
    @Parameter(name = "Authorization", description = "认证信息", required = true , in = ParameterIn.HEADER)
    @Parameter(name = "X-Device-Id", description = "设备ID", required = true , in = ParameterIn.HEADER)
    public Result<EmojiPackResponse> getUserEmojiPack(HttpServletRequest request) {
        Long userId = RequestUtils.getUserId(request);
        return Result.success(emojiPackService.getUserEmojiPack(userId));
    }


    @Operation(summary = "获取用户表情包项列表", description = "根据表情包ID获取当前用户的表情包项列表")
    @ApiResponse(
            responseCode = "200",
            description = "成功",
            content = @Content(
                    mediaType = "application/json",
                    array = @ArraySchema(
                            schema = @Schema(implementation = EmojiPackResponse.class, description = "表情包列表")
                    )
            )
    )
    @GetMapping("/item")
    @AuthCheck
    @Parameter(name = "Authorization", description = "认证信息", required = true , in = ParameterIn.HEADER)
    @Parameter(name = "X-Device-Id", description = "设备ID", required = true , in = ParameterIn.HEADER)
    public Result<List<EmojiPackItemResponse>> listUserEmojisByPackId(HttpServletRequest request) {
        Long userId = RequestUtils.getUserId(request);
        List<EmojiPackItemResponse> emojiPacks = emojiPackService.listUserEmojis(userId);
        return Result.success(emojiPacks);
    }

    @Operation(summary = "上传新的收藏表情包", description = "上传新的收藏表情包")
    @ApiResponse(
            responseCode = "200",
            description = "成功",
            content = @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = EmojiPackItemCreateResponse.class ,description = "更新时间戳")
            )
    )
    @AuthCheck
    @PostMapping(value = "/item" , consumes = "multipart/form-data")
    @Parameter(name = "Authorization", description = "认证信息", required = true , in = ParameterIn.HEADER)
    @Parameter(name = "X-Device-Id", description = "设备ID", required = true , in = ParameterIn.HEADER)
    public Result<EmojiPackItemCreateResponse> uploadEmojiPack(@RequestPart("file") MultipartFile file , HttpServletRequest request) {
        Long userId = RequestUtils.getUserId(request);
        return Result.success(emojiPackService.uploadEmojiPack(file, userId));
    }


    @Operation(summary = "删除收藏的表情包项", description = "删除收藏的表情包项")
    @ApiResponse(
            responseCode = "200",
            description = "成功",
            content = @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = EmojiPackItemUpdateResponse.class ,description = "更新时间戳")
            )
    )
    @DeleteMapping("/item/{emojiId}")
    @AuthCheck
    @Parameter(name = "Authorization", description = "认证信息", required = true , in = ParameterIn.HEADER)
    @Parameter(name = "X-Device-Id", description = "设备ID", required = true , in = ParameterIn.HEADER)
    public Result<EmojiPackItemUpdateResponse> deleteEmojiItem(@Parameter(name = "emojiId" , in = ParameterIn.PATH , required = true, description = "表情包ID") @PathVariable Long emojiId , HttpServletRequest request) {
        Long userId = RequestUtils.getUserId(request);
        return Result.success(emojiPackService.deleteEmojiItem(emojiId, userId));
    }


    @Operation(summary = "更新收藏的表情包项的排序", description = "更新收藏的表情包项的排序")
    @ApiResponse(
            responseCode = "200",
            description = "成功",
            content = @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = EmojiPackItemUpdateResponse.class ,description = "更新时间戳")
            )
    )
    @PutMapping("/item")
    @AuthCheck
    @Parameter(name = "Authorization", description = "认证信息", required = true , in = ParameterIn.HEADER)
    @Parameter(name = "X-Device-Id", description = "设备ID", required = true , in = ParameterIn.HEADER)
    public Result<EmojiPackItemUpdateResponse> updateEmojiItem(@RequestBody @Valid EmojiPackItemUpdateRequest req, HttpServletRequest request) {
        Long userId = RequestUtils.getUserId(request);
        return Result.success(emojiPackService.updateEmojiItem(req, userId));
    }

}
